<?php 

class WalletTransaction extends Eloquent{
	

	// 流转方向(direction)
	const DIRECTION_OUT = 'out';	// 转出
	const DIRECTION_IN = 'in';		// 转入
	
	// 流转状态(status)
	const STATUS_SUCCESS = 'success';	// 成功
	const STATUS_FAIL = 'fail';			// 失败
	const STATUS_WAIT = 'wait';			// 等待
	
	// 交易类型(type)
	const TYPE_REWARD = 'reward';			// 送心意
	const TYPE_QUESTION = 'question';		// 咨询
	const TYPE_PAY_TEXT = 'pay_text';		// 付费图文咨询
	const TYPE_PAY_PHONE = 'pay_phone';		// 付费电话咨询
	const TYPE_PAY_COMPANY = 'pay_company';	// 付费法律顾问
	const TYPE_RECHARGE = 'recharge';		// 充值
	const TYPE_WITHDRAW = 'withdraw';		// 提现
	
	public function getRecord(){
		$record = [];
		$record = $this->toArray();
		$record['type'] = $this->getType();
		$record['status'] = $this->getStatus();
		$record['direction'] = $this->getDirection();
		$record['description'] = $this->description;
		$record['sum'] = round($this->sum);
		return $record;
	}
	
	public function getType(){
		switch($this->type){
			case self::TYPE_REWARD:
				return "送心意";
				break;
			case self::TYPE_QUESTION:
				return "付费咨询";
				break;
			case self::TYPE_PAY_TEXT:
				return "付费图文咨询";
				break;
			case self::TYPE_PAY_PHONE:
				return "付费电话咨询";
				break;
			case self::TYPE_PAY_COMPANY:
				return "付费法律顾问";
				break;
			case self::TYPE_RECHARGE:
				return "充值";
				break;
			case self::TYPE_WITHDRAW:
				return "提现";
				break;
		}
	}
	
	public function getStatus(){
		switch($this->status){
			case self::STATUS_SUCCESS:
				return "成功";
				break;
			case self::STATUS_FAIL:
				return "失败";
				break;
			case self::STATUS_WAIT:
				return "处理中";
				break;
		}
	}
	
	public function getDirection(){
		switch($this->direction){
			case self::DIRECTION_OUT:
				return "-";
				break;
			case self::DIRECTION_IN:
				return "+";
				break;
		}
	}
	
	/*
	 * 查找一条记录
	 */
	public static function getWalletTransaction($user_id, $type, $order_id, $direction){
		$walletTransaction = WalletTransaction::where('user_id', $user_id)
 												->where('type', $type)
 												->where('order_no', $order_id)
 												->where('direction', $direction)
												->first();
		return $walletTransaction;
		
	}
	
	/**
	 * 创建并保存该账目记录
	 */
	public static function storeWalletTransaction($data){
		$result = new WalletTransaction();
				$result->user_id = $data['user_id'];
				$result->type = $data['type'];
				$result->direction = $data['direction'];
				if (isset($data['order_no'])) {
					$result->order_no = $data['order_no'];
				}
				if (isset($data['associate_id'])) {
					$result->associate_id = $data['associate_id'];
				}
				$result->status = $data['status'];
				$result->sum = $data['sum'];
				$result->description = $data['description'];
				$result->result = $data['result'];
				$result->save();
	}

	public static function createOrder($order_type, $order_no, $user_id, $money, $message) {
		$dataSend = array(
			'user_id' => $user_id,
			'type' => $order_type,
			'direction' => self::DIRECTION_OUT,
			'order_no' => $order_no, 
			'status' => self::STATUS_SUCCESS,
			'sum' => $money,
			'description' => $message,
			'result' => NULL
		);
		self::storeWalletTransaction($dataSend);
	}


	/**
	 * 创建一条送心意账目的双向记录
	 * @param $from_user_id
	 * @param $user_id
	 * @param $money
	 * @param $message
	 * @param null $order_no
	 * @param $channel
	 */
	public static function createReward($from_user_id, $user_id, $money, $message, $order_no=null, $channel){
		$dataSend = array(
				'user_id' => $from_user_id,
				'type' => self::TYPE_REWARD,
				'direction' => self::DIRECTION_OUT,
				'partner_user_id' => $user_id,
				'order_no' => $order_no,
				'status' => self::STATUS_SUCCESS,
				'sum' => $money,
				'description' => $message['send'],
				'result' => NULL,
		        'channel' => $channel
			);
		
		$dataRecive = array(
				'user_id' => $user_id,
				'type' => self::TYPE_REWARD,
				'direction' => self::DIRECTION_IN,
				'partner_user_id' => $from_user_id,
				'order_no' => $order_no,
				'status' => self::STATUS_SUCCESS,
				'sum' => $money,
				'description' => $message['recive'],
				'result' => NULL,
		        'channel' => $channel
			);
		DB::transaction(function() use($dataSend, $dataRecive){
			self::storeWalletTransaction($dataSend);
			self::storeWalletTransaction($dataRecive);
		});
	}
	
	/**
	 * 充值
	 */
	public static function createRecharge($user_id, $order_no, $money, $message, $status, $channel){
		$data = array(
				'user_id' => $user_id,
				'type' => self::TYPE_RECHARGE,
				'direction' => self::DIRECTION_IN,
				'order_no' => $order_no,
				'status' => $status,
				'sum' => $money,
				'description' => $message,
				'result' => NULL,
		        'channel' => $channel
		);
		self::storeWalletTransaction($data);
	}
	
	/**
	 * 提现记录
	 */
	public static function createWithdraw($withdraw_id, $money, $message){
		$dataSend = array(
				'user_id' => Auth::user()->id,
				'type' => self::TYPE_WITHDRAW,
				'direction' => self::DIRECTION_OUT,
				'associate_id' => $withdraw_id,
				'status' => self::STATUS_WAIT,
				'sum' => $money,
				'description' => $message,
				'result' => NULL
		);
		self::storeWalletTransaction($dataSend);
	}
	
	/**
	 * 统计某个律师收到的心意数
	 */
	public static function rewardCount($user_id){
		return WalletTransaction::where('direction', self::DIRECTION_IN)->where('user_id', $user_id)->where('type', self::TYPE_REWARD)->count();
	}
}
